package topcbl.taxi;

import topcbl.util.GPSTracker;
import topcbl.util.StringUtils;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class AddFavoriteDialog extends Dialog {
	Context context;
	String address;
	// view
	TextView tvAddress;
	EditText etDetails, etNumber, etName;
	Button btnBack, btnAddFavorite;
	double lat, lng;

	public AddFavoriteDialog(Context context, double lat, double lng) {
		super(context, R.style.MyDialogTheme);
		this.context = context;
		this.lat = lat;
		this.lng = lng;
		address = StringUtils.eliminateNumeral(GPSTracker
				.getAddressFromCoordinate(lat, lng, context));
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// animation
		getWindow().getAttributes().windowAnimations = R.style.DialogAnimation;
		// requestWindowFeature(Window.FEATURE_NO_TITLE);
		requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
		setContentView(R.layout.dialog_add_favorite);
		getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,
				R.layout.dialog_title_bar_add_favorite);
		// change title divider
		int titleDividerId = context.getResources().getIdentifier(
				"titleDivider", "id", "android");
		View titleDivider = getWindow().getDecorView().findViewById(
				titleDividerId);
		titleDivider.setBackgroundColor(context.getResources().getColor(
				R.color.orange_trans)); // change divider color

		// init
		tvAddress = (TextView) findViewById(R.id.dialog_addfavorite_address);
		etDetails = (EditText) findViewById(R.id.dialog_addfavorite_details);
		etNumber = (EditText) findViewById(R.id.dialog_addfavorite_number);
		etName = (EditText) findViewById(R.id.dialog_addfavorite_name);
		btnBack = (Button) findViewById(R.id.dialog_addfavorite_btnBack);
		btnAddFavorite = (Button) findViewById(R.id.dialog_addfavorite_btnCallTaxi);
		// setup
		tvAddress.setText(address);
		btnBack.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				dismiss();
			}
		});
		btnAddFavorite.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				String number = etNumber.getText().toString();
				String des = etDetails.getText().toString();
				String name = etName.getText().toString();
				if (name.length() > 0) {
					if (number.length() > 0 || des.length() > 0) {
						MainActivity.myDatabase.addFavorite(name, address,
								number, des, lat, lng);
						dismiss();
						Toast.makeText(
								context,
								getContext().getString(
										R.string.toast_add_favorite_success),
								Toast.LENGTH_SHORT).show();
					} else {
						Toast.makeText(
								context,
								getContext().getString(
										R.string.toastCallTaxiError),
								Toast.LENGTH_SHORT).show();
					}
				} else {
					Toast.makeText(context,
							getContext().getString(R.string.toastFavoriteName),
							Toast.LENGTH_SHORT).show();
				}
			}
		});

	}

}
